package com.kl.forum.service;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.kl.forum.entity.*;
import com.kl.forum.enums.ResultCode;
import com.kl.forum.enums.SearchEnums;
import com.kl.forum.util.BASE64Util;
import com.kl.forum.util.ResultInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.kl.forum.service.CircleService.adAndArt6;
import static com.kl.forum.util.ListUtil.randomTopic;

@Service
public class HomePageService {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private  ArticleCollectService articleCollectService;


    @Autowired
    private  MemberService memberService;

    @Autowired
    private ColumnDicService columnDicService;

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FunctionDicService functionService;

    @Autowired
    private TagDicService tagDicService;

    @Autowired
    private  ArticleService articleService;

    @Autowired
    private  AdListService adListService;

    @Autowired
    private  ArticleLightService articleLightService;

    @Autowired
    private ArticleCommentService articleCommentService;

    @Autowired
    private  ArticleCommentLightService articleCommentLightService;

    @Autowired
    private  ArticleCommentReplyService articleCommentReplyService;

    @Autowired
    private  ArticleCommentReplyLightService articleCommentReplyLightService;

    @Autowired
    private  HealthyNumService healthyNumService;

    @Autowired
    private  UserHealthyService userHealthyService;

    @Autowired
    private  HealthyDicService healthyDicService;

    @Autowired
    private TagKindDicService tagKindDicService;

    @Autowired
    private CircleArticleService circleArticleService;

    /**
     * 展示栏目
     * @param userId
     * @return
     */
    public ResultInfo  showColumn(Integer userId){
        ResultInfo re =new ResultInfo();
        Map map =new HashMap();
        //是否为默认栏目
        RelUserColumn relUserColumn=columnDicService.getUserColumnByUserId(userId);
        if (relUserColumn==null){
            try{
                 List<Map> list_data=columnDicService.getAllColumn();
                 String data=JSON.toJSONString(list_data).trim();
                 if (userId!=null){
                     columnDicService.insert(userId,data);
                 }
                map.put("columnData",list_data);
                map.put("columnDataHide",null);
                re.setData(map);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }catch (Exception e){
                e.printStackTrace();
            }
        }

            map.put("columnData",JSON.parseArray(relUserColumn.getColumnData()));
            map.put("columnDataHide",JSON.parseArray(relUserColumn.getColumnDataHide()));
            re.setData(map);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re ;
    }

    /**
     * 编辑栏目
     * @return
     */
    public ResultInfo editColumn(Integer userId,String columnData,String columnDataHide){
        ResultInfo re= new ResultInfo();
        try {
            columnData= BASE64Util.decode(columnData);
            columnDataHide=BASE64Util.decode(columnDataHide);
            RelUserColumn relUserColumn=columnDicService.getUserColumnByUserId(userId);
            columnDicService.update(userId,columnData, columnDataHide,relUserColumn.getId());
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 展示Banner
     * @return
     */
    public ResultInfo showBanner(){
        ResultInfo re =new ResultInfo();
        Map map=new HashMap();
//        map.put("num",4);
        try {
            List<Map> data= bannerService.showBanner();
//          广告类型  1：启动页广告2：首页banner图3：科室栏目页banner图4：帖子列表中间广告条5：栏目内页广告条6：栏目内部商务通悬窗7：咨询文章底部广告位
            List<Map> data1=adListService.findAdByType(2);
            data.addAll(data1);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 展示功能菜单
     * @return
     */
    public  ResultInfo showFunction(){
        ResultInfo re =new ResultInfo();
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("num",4);
            List<Map> data=functionService.showFunction(map);

            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public  ResultInfo findAllFunction(){
        ResultInfo re =new ResultInfo();
        try {
            Map<String,Object> map=new HashMap<>();
            map.put("num",null);
            List<Map> data=functionService.findAllFunction(map);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


    /**
     * 知识专题 3个tag菜单
     * @return
     */
    public ResultInfo showTag(){
        ResultInfo re =new ResultInfo();
        try {
            Map<String,Object> map = new HashMap<>();
            map.put("num",3);
            List<Map> data= tagDicService.showTag(map);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 知识专题多个tag菜单
     * @return
     */
    public ResultInfo findTagAll(){
        ResultInfo re =new ResultInfo();
        try {
            List<Map> data= tagDicService.findTagAll();
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 每日精选
     * @return
     */
    public  ResultInfo dailySelection(int pageNum,int pageSize,int adNum){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo<Article> data=  articleService.dailySelection(pageNum,pageSize);
            List<Map> list=adListService.findAdvertisement();
            re.setData(adAndArt6(list,data));
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 通过栏目ID查看文章列表
     * @param columnId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultInfo findArticleByColumnId(Integer columnId,Integer pageNum ,Integer pageSize,Integer adNum){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo<Article> data= articleService.findArticleByColumnId(columnId,pageNum,pageSize);
            List<Map> list=adListService.findAdvertisement();
            re.setData(adAndArt6(list,data));
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 通过Tag_id寻找文章列表
     * @param tagId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultInfo findArticleByTagId(Integer tagId,Integer pageNum ,Integer pageSize){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo<Article> data= articleService.findArticleByTagId(tagId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 查找所有广告并随机取出N条数据
     * @return
     */
    public  ResultInfo findAdvertisement(int adNum){
        ResultInfo re=new ResultInfo();
        try {
            List<Map> list=adListService.findAdvertisement();
            List<Map> data= randomTopic(list,adNum);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 文章详情
     * @return
     */
    public ResultInfo   articleDetail(Integer articleId,Integer userId){
        ResultInfo re=new ResultInfo();
        try {
            Article data= articleService.articleDetail(articleId);

//            ValueOperations valueOperations =redisTemplate.opsForValue();
            boolean hashKeyBoolean = stringRedisTemplate.opsForHash().hasKey("healthyNumReadCount",data.getHealthNumId().toString());
            //如果没有值
           if (!hashKeyBoolean){
               stringRedisTemplate.opsForHash().put("healthyNumReadCount",data.getHealthNumId().toString(),"1");
//               valueOperations.increment("healthyArticle"+data.getHealthNumId());
           }else {
//               Object value=  stringRedisTemplate.opsForHash().get("healthyNumReadCount",data.getHealthNumId().toString());
               stringRedisTemplate.opsForHash().increment("healthyNumReadCount",data.getHealthNumId().toString(),1);
           }
           if (!stringRedisTemplate.opsForHash().hasKey("articleViewNumCount",String.valueOf(articleId))){
               stringRedisTemplate.opsForHash().put("articleViewNumCount",String.valueOf(articleId),"1");
           }else {
               stringRedisTemplate.opsForHash().increment("articleViewNumCount",String.valueOf(articleId),1);
           }
            if (!stringRedisTemplate.opsForHash().hasKey("articleLightNumCount",String.valueOf(articleId))){
                stringRedisTemplate.opsForHash().put("articleLightNumCount",String.valueOf(articleId),"0");
            }
//            data.setViewNum(data.getViewNum()+1);
//            articleService.update(data);
            ArticleLight articleLight= articleLightService.findLightRecord(articleId,userId);
            if (articleLight!=null){
                //
                data.setLightStatus(articleLight.getStatus());
            }else{
                //未点赞
                data.setLightStatus(0);
            }
//            暂时没有收藏
//            ArticleCollect collect=articleCollectService.findOne(userId,articleId);
//            if (collect!=null){
//                data.setIsCollect(collect.getStatus());
//            }else {
//                //未收藏
//                data.setIsCollect(0);
//            }
            int commentNum= articleService.countCommentNum(articleId);
            data.setCommentNum(commentNum);
            data.setLightNum(Integer.valueOf((String) stringRedisTemplate.opsForHash().get("articleLightNumCount",String.valueOf(articleId))));
            data.setViewNum(Integer.valueOf((String) stringRedisTemplate.opsForHash().get("articleViewNumCount",String.valueOf(articleId))));
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 文章点赞
     * @param articleId
     * @return
     */
    public ResultInfo   articleLight(int articleId,int userId){
        ResultInfo re=new ResultInfo();
        try {


//            Article data= articleService.articleDetail(articleId);
//            data.setLightNum(data.getLightNum()+1);
//            articleService.update(data);
            //是否有点赞记录
            ArticleLight articleLight= articleLightService.findLightRecord(articleId,userId);
            //用户 文章 点赞 关联表记录
            if(articleLight==null){
                ArticleLight light =new ArticleLight();
                light.setArticleId(articleId);
                light.setUserId(userId);
                light.setStatus(1);
             if (!stringRedisTemplate.opsForHash().hasKey("articleLightNumCount",String.valueOf(articleId))){
                stringRedisTemplate.opsForHash().put("articleLightNumCount",String.valueOf(articleId),"1");
            }else {
                stringRedisTemplate.opsForHash().increment("articleLightNumCount",String.valueOf(articleId),1);
            }
                articleLightService.insertSelective(light);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else {
                if (articleLight.getStatus()==1){
                    re.setData("您已经点过赞");
                }
                articleLight.setStatus(1);
                articleLightService.update(articleLight);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return   re;
            }

        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 文章取消点赞
     * @param articleId
     * @param userId
     * @return
     */
    public ResultInfo articleDark(Integer articleId,Integer userId){
        ResultInfo re= new ResultInfo();
        try {
            Article data= articleService.articleDetail(articleId);
            if (data.getLightNum()-1<=0){
                data.setLightNum(0);
            }
            data.setLightNum(data.getLightNum()-1);
            articleService.update(data);
            //是否有点赞记录
            ArticleLight articleLight= articleLightService.findLightRecord(articleId,userId);
            //用户 文章 点赞 关联表记录
            ArticleLight light =new ArticleLight();
            light.setArticleId(articleId);
            light.setUserId(userId);
            light.setId(articleLight.getId());
            light.setStatus(0);
            articleLightService.update(light);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**
     * 文章评论列表
     * @param articleId
     * @return
     */
    public ResultInfo findArticleCommentBySort(Integer userId,Integer articleId,Integer pageNum ,Integer pageSize,String sort){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo<Map> data= articleCommentService.findArticleCommentBySort(userId,articleId,pageNum,pageSize,sort);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;


    }
    /**
     * 文章评论列表
     * @param articleId
     * @return
     */
    public ResultInfo findArticleCommentByLightNum(Integer userId,Integer articleId,Integer pageNum ,Integer pageSize){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo<Map> data= articleCommentService.findArticleCommentByLightNum(userId,articleId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;


    }



    /**
     * 发表评论
     * @param userId
     * @param articleId
     * @return
     */
    public  ResultInfo addArticleComment(Integer userId,Integer articleId,String content){
        ResultInfo re = new ResultInfo();
        try {
            ArticleComment articleComment = new ArticleComment();
            articleComment.setArticleId(articleId);
            articleComment.setContent(content);
            articleComment.setCreateTime(new Date());
            articleComment.setUserId(userId);
            articleComment.setLightNum(0);
            articleComment.setReply(0);
            articleCommentService.addArticleComment(articleComment);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }



    /**
     * 用户删除自己的评论 删除
     * @param
     * @param articleCommentId
     * @return
     */
    public  ResultInfo delArticleComment(Integer articleCommentId ){
        ResultInfo re= new ResultInfo();
        try {
            articleCommentService.delArticleComment(articleCommentId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 文章评论点赞
     * @param userId
     * @param articleCommentId
     * @return
     */
    public ResultInfo articleCommentLight(Integer articleCommentId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
//   因为要排序所以没有存到redis
//
//            if (!stringRedisTemplate.opsForHash().hasKey("articleCommentLightNum",articleCommentId.toString())){
//                stringRedisTemplate.opsForHash().put("articleCommentLightNum",articleCommentId.toString(),"1");
//            }else {
//                stringRedisTemplate.opsForHash().increment("articleCommentLightNum",articleCommentId.toString(),1);
//            }
            ArticleComment   articleComment= articleCommentService.findArticleCommentById(articleCommentId);
            articleComment.setLightNum(articleComment.getLightNum()+1);
            articleCommentService.update(articleComment);
            //查询是否有记录
            ArticleCommentLight ar= articleCommentLightService.findArticleCommentLightRecord(articleCommentId,userId);
            if (ar!=null){
                if (ar.getStatus()==1){
                    re.setData("您已经点过赞");
                }
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return   re;
            }else {
                ArticleCommentLight ar2= new ArticleCommentLight();
                ar2.setCommentId(articleCommentId);
                ar2.setStatus(1);
                ar2.setUserId(userId);
                articleCommentLightService.insert(ar2);
            }
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }




    /**
     * 文章评论点灭
     * @param userId
     * @param articleCommentId
     * @return
     */
    public ResultInfo articleCommentDark(Integer articleCommentId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            ArticleComment   articleComment= articleCommentService.findArticleCommentById(articleCommentId);
            articleComment.setLightNum(articleComment.getLightNum()-1);
            articleCommentService.update(articleComment);
            //查询是否有记录
            ArticleCommentLight ar= articleCommentLightService.findArticleCommentLightRecord(articleCommentId,userId);
            ar.setStatus(0);
            articleCommentLightService.update(ar);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**lightStatus 是否点赞过
     * @param commentId 文章一级评论ID    点赞  时间  倒叙
     * @param
     * @return
     */
    public ResultInfo findArticleCommentReply(Integer userId,Integer commentId ,Integer pageNum ,Integer pageSize){
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=articleCommentReplyService.findArticleCommentReply(userId,commentId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }




    /**
     * 发表评论回复
     * @param userId
     * @param commentId
     * @return
     */
    public  ResultInfo addArticleCommentReply(Integer userId,String userName,Integer commentId,String content,int articleId,String commentName ,Integer commentNameId){
        ResultInfo re = new ResultInfo();
        try {
            ArticleCommentReply articleCommentReply=articleCommentReplyService.addArticleCommentReply(commentId, content, userId, commentName, userName, articleId, commentNameId);
            re.setData(articleCommentReply);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }



    /**
     * 用户删除自己的评论 删除
     * @param
     * @param replyId
     * @return
     */
    public  ResultInfo delArticleCommentReply(Integer replyId){
        ResultInfo re= new ResultInfo();
        try {
            articleCommentReplyService.delArticleCommentReply(replyId);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 文章评论回复点赞
     * @param userId
     * @param replyId
     * @return
     */
    public ResultInfo articleCommentReplyLight(Integer replyId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            ArticleCommentReply   articleCommentReply= articleCommentReplyService.findArticleCommentById(replyId);
            articleCommentReply.setLightNum(articleCommentReply.getLightNum()+1);
            articleCommentReplyService.update(articleCommentReply);
            //查询是否有记录
            ArticleCommentReplyLight ar= articleCommentReplyLightService.findArticleCommentReplyLightRecord(replyId,userId);
            if (ar!=null){
                ar.setStatus(1);
                articleCommentReplyLightService.update(ar);
            }else {
                ArticleCommentReplyLight ar2= new ArticleCommentReplyLight();
                ar2.setReplyId(replyId);
                ar2.setStatus(1);
                ar2.setUserId(userId);
                articleCommentReplyLightService.insert(ar2);
            }
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }




    /**
     * 文章评论回复点灭
     * @param userId
     * @param replyId
     * @return
     */
    public ResultInfo articleCommentReplyDark(Integer replyId, Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            ArticleCommentReply   articleCommentReply= articleCommentReplyService.findArticleCommentById(replyId);
            articleCommentReply.setLightNum(articleCommentReply.getLightNum()-1);
            articleCommentReplyService.update(articleCommentReply);
            ArticleCommentReplyLight ar= articleCommentReplyLightService.findArticleCommentReplyLightRecord(replyId,userId);
            ar.setStatus(0);
            articleCommentReplyLightService.update(ar);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 关注
     * @param userId
     * @return
     */
    public ResultInfo findHealthy(Integer userId) {
        ResultInfo re= new ResultInfo();
        try {
            List<Map> list=healthyNumService.findHealthy(userId);
            if (list==null){
                List<Map> list1= healthyNumService.findHealthyAll();
                List data =randomTopic(list1,4);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return re;
            }else{
                //不为null
               int countNum= memberService.countUserHealthyNum(userId);
                re.setData(countNum);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     *  按照用户关注健康号显示
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultInfo findHealthyArticleByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo  data = articleService.findHealthyArticleByUserId( userId,  pageNum,  pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }

    /**关注：
     * 我的关注健康号列表
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultInfo findMyHealthyNumByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo data=healthyNumService.findMyHealthyNumByUserId(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;



    }

    /**
     * 关注：
     * 健康号的文章列表
     * @param healthyId
     * @param pageNum
     * @param pageSize
     * @return
     */
    public ResultInfo findArticleByHealthyNumId(Integer healthyId, Integer pageNum, Integer pageSize) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo data=articleService.findArticleByHealthyNumId(healthyId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 关注：
     * 去关注
     * @param userId
     * @param healthyId
     * @return
     */
    public ResultInfo addUserHealthy(Integer userId, Integer healthyId) {
        ResultInfo re = new ResultInfo();
        try {
            UserHealthy userHealthy=userHealthyService.findUserHealthyBy(userId,healthyId);
            HealthyNum healthyNum= healthyNumService.findHealthyById(healthyId);
            healthyNum.setFollowNum(healthyNum.getFollowNum()+1);
            if(userHealthy!=null){
                userHealthy.setStatus(1);
                userHealthyService.update(userHealthy);
                healthyNumService.update(healthyNum);
                return  re;
            }else {
                userHealthyService.addUserHealthy(userId,healthyId);
                healthyNumService.update(healthyNum);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 关注：
     * 取消关注
     * @param userId
     * @param healthyId
     * @return
     */
    public ResultInfo updateUserHealthy(Integer userId, Integer healthyId) {
        ResultInfo re = new ResultInfo();
        try {
            UserHealthy userHealthy=userHealthyService.findUserHealthyBy(userId,healthyId);
            HealthyNum healthyNum= healthyNumService.findHealthyById(healthyId);
            healthyNum.setFollowNum(healthyNum.getFollowNum()-1);
            userHealthy.setStatus(0);
            userHealthyService.update(userHealthy);
            healthyNumService.update(healthyNum);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 健康号类型字典表菜单
     * @return
     */
    public ResultInfo findHealthyDic() {
        ResultInfo re= new ResultInfo();
        try {
            List<Map> data= healthyDicService.findHealthyDic();
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 通过类型寻找未关注健康号
     * @param type
     * @return
     */
    public ResultInfo findHealthyNumByType(Integer type, Integer userId,Integer pageNum ,Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= healthyNumService.findHealthyNumByType(type,userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }

        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 健康号详情
     * @param healthyId
     * @return
     */
    public ResultInfo healthyNumDetail(Integer healthyId) {
        ResultInfo re=new ResultInfo();
        try {
            HealthyNum data=healthyNumService.findHealthyById(healthyId);
            if (stringRedisTemplate.opsForHash().get("healthyNumReadCount",healthyId.toString())!=null){
                data.setBrowseNum(stringRedisTemplate.opsForHash().get("healthyNumReadCount",healthyId.toString()).toString());
            }else {
                data.setBrowseNum("0");
            }
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 收藏 文章
     * @return
     */
    public ResultInfo addArticleCollect(Integer userId, Integer articleId) {
        ResultInfo re= new ResultInfo();
        try {
            ArticleCollect collect=articleCollectService.findOne(userId,articleId);
            if(collect!=null){
                collect.setStatus(1);
                re.setData("已经收藏过了");
                articleCollectService.update(collect);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }else {
                articleCollectService.addArticleCollect(userId,articleId);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 取消收藏
     * @param userId
     * @param articleId
     * @return
     */
    public ResultInfo updateArticleCollect(Integer userId, Integer articleId) {
        ResultInfo re=new ResultInfo();
        try {
            ArticleCollect collect=articleCollectService.findOne(userId,articleId);
            collect.setStatus(0);
            articleCollectService.update(collect);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 查找我的收藏
     * @return
     */
    public ResultInfo findArticleCollect(Integer userId,Integer pageNum,Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= articleService.findArticleCollect(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    /**
     * 根据tagId寻找百度百度的种类
     * @param tagId
     * @return
     */
    public ResultInfo findKindByTagId(Integer tagId,Integer pageNum ,Integer pageSize) {
        ResultInfo re= new ResultInfo();
        try {
            PageInfo data=tagKindDicService.findTagKind(tagId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }




    /**
     * 搜索
     * @return
     */
    public ResultInfo search(String keyWord,String type,Integer pageNum,Integer pageSize,Integer userId) {
        ResultInfo re=new ResultInfo();
        try {
            if (type.equals(SearchEnums.文章.getCode()) ){
                PageInfo data= articleService.searchArticle(keyWord,pageNum,pageSize,"1",userId);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
            if (type.equals(SearchEnums.视频.getCode())){
                PageInfo data= articleService.searchArticle(keyWord,pageNum,pageSize,"2",userId);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
            if (type.equals(SearchEnums.帖子.getCode())){
                PageInfo data= circleArticleService.searchCircleArticle(keyWord,pageNum,pageSize);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
            if (type.equals(SearchEnums.用户.getCode())){
                PageInfo data= memberService.searchMemberName(keyWord,pageNum,pageSize);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
            if (type.equals(SearchEnums.健康号.getCode())){
                PageInfo data= healthyNumService.searchHealthyName(keyWord,pageNum,pageSize);
                re.setData(data);
                re.setMessage(ResultCode.SUCCESS.getMsg());
                re.setCode(ResultCode.SUCCESS.getCode());
                return  re;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;

    }


    /**
     * 百科详情
     * @param id
     * @return
     */
    public ResultInfo findKindByKindId(Integer id) {
        ResultInfo re =new ResultInfo();
        try {
            TagKindDic data=tagKindDicService.findKindByKindId(id);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findKindByArticle(Integer kindId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data= articleService.findKindByArticle(kindId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }

        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findKindByCircleArticle(Integer kindId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=   articleService.findKindByCircleArticle(kindId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo recommendArticle(Integer pageNum, Integer pageSize,Integer articleId) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=   articleService.recommendArticle(pageNum,pageSize,articleId);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findCircleArticleCommentReplyList(Integer userId, Integer commentId,Integer pageNum ,Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            ArticleComment articleComment=   articleCommentReplyService.findCircleArticleCommentReplyList(userId,commentId,pageNum,pageSize);
            re.setData(articleComment);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }

    public ResultInfo findMyFollowUserCircle(Integer userId, Integer pageNum, Integer pageSize) {
        ResultInfo re=new ResultInfo();
        try {
            PageInfo data=   circleArticleService.findMyFollowUserCircle(userId,pageNum,pageSize);
            re.setData(data);
            re.setMessage(ResultCode.SUCCESS.getMsg());
            re.setCode(ResultCode.SUCCESS.getCode());
            return  re;
        }catch (Exception e){
            e.printStackTrace();
        }
        re.setMessage(ResultCode.INTERNAL_SERVER_ERROR.getMsg());
        re.setCode(ResultCode.INTERNAL_SERVER_ERROR.getCode());
        return  re;
    }


}
